home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_2 / vsrc.tar / voyager7_src / search.c < prev    next >
C/C++ Source or Header  |  1991-02-27  |  6KB  |  306 lines

  1. /*
  2. // Abstract:
  3. //    SEARCH---Searching
  4. //
  5. //    The Searhing module supports searching portions of memory for
  6. //    specified data.
  7. //
  8. // Author:
  9. //    Derek S. Nickel
  10. //
  11. // Creation date:
  12. //    6 January 1991
  13. //
  14. // History:
  15. // V01-001    Derek S. Nickel         6-JAN-1991
  16. //    Extracted into separte module.
  17. //
  18. */
  19.  
  20. #include <ctype.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <io.h>
  24.  
  25. #include "memory.h"
  26. #include "modes.h"
  27. #include "pager.h"
  28.  
  29. static void search_port(int port_no, bin5_t start, bin5_t end,
  30. long *matches, char *rest);
  31.  
  32. static void search_port_prefix(int port_no, bin5_t start, bin5_t end,
  33. long *matches );
  34.  
  35. static void search_port_entry(int port_no, bin5_t start, bin5_t end,
  36. long *matches );
  37.  
  38. /***********************************************************************
  39.     Messages.
  40. ***********************************************************************/
  41.  
  42. #define vgr__nomatches \
  43. "%%VOYAGER-I-NOMATCHES, no matches found\n"
  44.  
  45. #define vgr__matched \
  46. "%%VOYAGER-I-MATCHED, found %ld matches\n"
  47.  
  48. /***********************************************************************
  49.     search_memory
  50. ***********************************************************************/
  51.  
  52. void search_memory(bin5_t start, bin5_t end, char *data)
  53. {
  54.     long matches = 0;
  55.  
  56.     data = strupr(data);
  57.  
  58.     if (strncmp(data,"PREFIX",strlen(data)) == 0) {
  59.  
  60.         if (ports[0].loaded)
  61.             search_port_prefix(0, start, end, &matches);
  62.  
  63.         if (ports[1].loaded)
  64.             search_port_prefix(1, start, end, &matches);
  65.  
  66.         if (ports[2].loaded)
  67.             search_port_prefix(2, start, end, &matches);
  68.  
  69.     } else if (strncmp(data,"ENTRY",strlen(data)) == 0) {
  70.  
  71.         if (ports[0].loaded)
  72.             search_port_entry(0, start, end, &matches);
  73.  
  74.         if (ports[1].loaded)
  75.             search_port_entry(1, start, end, &matches);
  76.  
  77.         if (ports[2].loaded)
  78.             search_port_entry(2, start, end, &matches);
  79.  
  80.     } else {
  81.  
  82.         if (*data == '\'') {
  83.             data++;
  84.         } else {
  85.             strrev(data);
  86.         }
  87.  
  88.         if (ports[0].loaded)
  89.             search_port(0, start, end, &matches, data);
  90.  
  91.         if (ports[1].loaded)
  92.             search_port(1, start, end, &matches, data);
  93.  
  94.         if (ports[2].loaded)
  95.             search_port(2, start, end, &matches, data);
  96.  
  97.     }
  98.  
  99.     /*
  100.     // Finish last line and print summary.
  101.     */
  102.  
  103.     if (matches % 10 != 0) putchar('\n');
  104.  
  105.     pager(0);
  106.     if (matches == 0)
  107.         printf(vgr__nomatches);
  108.     else
  109.         printf(vgr__matched, matches);
  110. }
  111.  
  112. /***********************************************************************
  113.     search_port
  114. ***********************************************************************/
  115.  
  116. static void search_port(int port_no, bin5_t start, bin5_t end,
  117. long *matches, char *data)
  118. {
  119.     char buf[513];
  120.     char *pos;
  121.     bin5_t found_at;
  122.     bin5_t min_adr = 0;
  123.     bin5_t max_adr = 0;
  124.     size_t len_data;
  125.     size_t offset;
  126.     int nibs;
  127.  
  128.     /*
  129.     // Define allowable search range for this port.
  130.     */
  131.  
  132.     min_adr = ports[port_no].min_adr;
  133.     max_adr = ports[port_no].max_adr;
  134.  
  135.     /*
  136.     // Force start amd end into range.
  137.     */
  138.  
  139.     if (start < min_adr) start = min_adr;
  140.     if (start > max_adr) start = max_adr;
  141.     if (end < min_adr) end = min_adr;
  142.     if (end > max_adr) end = max_adr;
  143.  
  144.     /*
  145.     // Verify correct sequencing.
  146.     */
  147.  
  148.     if (start > end) {
  149.         bin5_t tmp;
  150.         tmp = start;
  151.         start = end;
  152.         end = tmp;
  153.     }
  154.  
  155.     /*
  156.     // Search memory in chunks.
  157.     */
  158.  
  159.     len_data = strlen(data);
  160.  
  161.     while (start < end - len_data) {
  162.  
  163.         /* putchar('.'); */
  164.  
  165.         SetWorkPtr(start);
  166.         nibs = min(512L, end - start + 1L);
  167.         GetNNibbles(buf,nibs);
  168.         pos = buf;
  169.  
  170.         while (pos = strstr(pos,data)) {
  171.             if (*matches % 10 == 0) pager(0);
  172.             (*matches)++;
  173.             offset = pos - buf;
  174.             found_at = start + offset;
  175.             printf(" %05lX ", found_at);
  176.             if (*matches % 10 == 0) putchar('\n');
  177.             pos++;
  178.         }
  179.  
  180.         offset = nibs - len_data + 1;
  181.         start += offset;
  182.     }
  183. }
  184.  
  185. /***********************************************************************
  186.     search_port_prefix
  187. ***********************************************************************/
  188.  
  189. static void search_port_prefix(int port_no, bin5_t start, bin5_t end,
  190. long *matches)
  191. {
  192.     bin5_t min_adr = 0;
  193.     bin5_t max_adr = 0;
  194.     bin5_t prefix;
  195.  
  196.     /*
  197.     // Define allowable search range for this port.
  198.     */
  199.  
  200.     min_adr = ports[port_no].min_adr;
  201.     max_adr = ports[port_no].max_adr;
  202.  
  203.     /*
  204.     // Force start amd end into range.
  205.     */
  206.  
  207.     if (start < min_adr) start = min_adr;
  208.     if (start > max_adr) start = max_adr;
  209.     if (end < min_adr) end = min_adr;
  210.     if (end > max_adr) end = max_adr;
  211.  
  212.     /*
  213.     // Verify correct sequencing.
  214.     */
  215.  
  216.     if (start > end) {
  217.         bin5_t tmp;
  218.         tmp = start;
  219.         start = end;
  220.         end = tmp;
  221.     }
  222.  
  223.     /*
  224.     // Search memory.
  225.     */
  226.  
  227.     while (start < end - 5) {
  228.  
  229.         /* putchar('.'); */
  230.  
  231.         SetWorkPtr(start);
  232.         prefix = get_bin5();
  233.  
  234.         if (prefix == start + 5) {
  235.             if (*matches % 10 == 0) pager(0);
  236.             (*matches)++;
  237.             printf(" %05lX ", start);
  238.             if (*matches % 10 == 0) putchar('\n');
  239.             start += 5;
  240.         } else {
  241.             start += 1;
  242.         }
  243.     }
  244. }
  245.  
  246. /***********************************************************************
  247.     search_port_entry
  248. ***********************************************************************/
  249.  
  250. static void search_port_entry(int port_no, bin5_t start, bin5_t end,
  251. long *matches)
  252. {
  253.     bin5_t min_adr = 0;
  254.     bin5_t max_adr = 0;
  255.     bin5_t prefix;
  256.  
  257.     /*
  258.     // Define allowable search range for this port.
  259.     */
  260.  
  261.     min_adr = ports[port_no].min_adr;
  262.     max_adr = ports[port_no].max_adr;
  263.  
  264.     /*
  265.     // Force start amd end into range.
  266.     */
  267.  
  268.     if (start < min_adr) start = min_adr;
  269.     if (start > max_adr) start = max_adr;
  270.     if (end < min_adr) end = min_adr;
  271.     if (end > max_adr) end = max_adr;
  272.  
  273.     /*
  274.     // Verify correct sequencing.
  275.     */
  276.  
  277.     if (start > end) {
  278.         bin5_t tmp;
  279.         tmp = start;
  280.         start = end;
  281.         end = tmp;
  282.     }
  283.  
  284.     /*
  285.     // Search memory.
  286.     */
  287.  
  288.     while (start < end - 5) {
  289.  
  290.         /* putchar('.'); */
  291.  
  292.         SetWorkPtr(start);
  293.         prefix = get_bin5();
  294.  
  295.         if ((prefix == start + 5) || (prefix == 0x02D9D)) {
  296.             if (*matches % 10 == 0) pager(0);
  297.             (*matches)++;
  298.             printf(" %05lX ", start);
  299.             if (*matches % 10 == 0) putchar('\n');
  300.             start += 5;
  301.         } else {
  302.             start += 1;
  303.         }
  304.     }
  305. }
  306.